package com.ohs.controller;

import com.ohs.pojo.User;
import com.ohs.service.IUserService;
import com.ohs.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    //去到登录页面
    @GetMapping("/toRegisterPage")
    public String toRegisterPage(){
        return "register-login";
    }

    //检查注册名
    @PostMapping("/checkRegisterName")
    @ResponseBody
    public Map<String, Object> checkRegisterName(String username){
        return this.iUserService.checkRegisterName(username);
    }

    //注册
    @PostMapping("/doRegister")
    @ResponseBody
    public JsonResult doRegister(User user, String confirmPwd){
        return this.iUserService.doRegister(user, confirmPwd);
    }

    //去到登录页面
    @GetMapping("/toLogin")
    public String toLogin(){
        return "register-login";
    }

    //检查登录名
    @PostMapping("/checkLoginName")
    @ResponseBody
    public JsonResult checkLoginName(String username){
        return this.iUserService.checkLoginName(username);
    }

    //登录
    @PostMapping("/doLogin")
    @ResponseBody
    public JsonResult doLogin(String username, String password, HttpSession session){
        return this.iUserService.doLogin(username, password, session);
    }

    //去到登录页面
    @GetMapping("/toIndex")
    public String toIndex(){
        return "index";
    }
}
